<?php

namespace app\api\model;

use think\Model;
use DateTime;
class BonusUsers extends Model
{

    public static function getList($where,$field,$page,$limit){
        $list = self::alias('bu')
            ->join("bonus b",'b.id=bu.bonus_id')
            ->where($where)
            ->field($field)
            ->limit(($page-1)*$limit,$limit)
            ->order("bu.id desc")
            ->select();
        foreach($list as &$v){
            $v['num'] = $v['num']-$v['ynum'];
            if($v['num'] < 0){
                $v['num'] = 0;
            }
            //到期时间
            $endtime = $v['endtime']-time();//秒
            if($endtime >= 86400){
                $endtime_text = (int)($endtime/86400).' 天后到期';
            }else{
                //小于一天
                if($endtime >= 3600){
                    $endtime_text = (int)($endtime/3600).' 小时后到期';
                }else{
                    //不足一小时
                    $endtime_text = ceil($endtime/60).' 分钟后到期';
                }
            }
            $v['endtime_text'] = $endtime_text;
        }
        $total = self::alias('bu')
            ->join("bonus b",'b.id=bu.bonus_id')
            ->where($where)
            ->count();
        return ['total'=>$total,'list'=>$list];
    }

    public static function getInfo($where,$field){
        $info = self::alias('bu')
            ->join("bonus b",'b.id=bu.bonus_id')
            ->where($where)
            ->field($field)
            ->find();
        if(isset($info['num'])){
            $info['num'] = $info['num']-$info['ynum'];
            if($info['num'] < 0){
                $info['num'] = 0;
            }
        }
        return $info;
    }
}
